package com.lc.pianostudent.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.adapter.DegreeAdapter;
import com.lc.pianostudent.adapter.PianoRecordAdapter;
import com.lc.pianostudent.bean.DegreeBean;
import com.lc.pianostudent.bean.PianoRecordBean;
import com.lc.pianostudent.conn.PostDegreeIndex;
import com.lc.pianostudent.conn.PostUserMyLevel;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/13.
 */

public class DegreeIntroduceActivity extends BaseActivity{
    private int width = 720;
    @BoundView(R.id.deep_pink) private TextView deep_pink;
    @BoundView(R.id.jindu_text) private TextView jindu_text;
    @BoundView(R.id.qian_pink) private TextView qian_pink;
    @BoundView(R.id.begin) private TextView begin;
    @BoundView(R.id.end) private TextView end;
    @BoundView(R.id.one) private ImageView one;
    @BoundView(R.id.two) private ImageView two;
    @BoundView(R.id.three) private ImageView three;
    @BoundView(R.id.four) private ImageView four;
    @BoundView(R.id.five) private ImageView five;
    public List<DegreeBean> list = new ArrayList<>();
    @BoundView(R.id.degree_recycle_view) private RecyclerView recyclerView;
    private DegreeAdapter degreeAdapter;
    @BoundView(R.id.xuefen_text) private TextView xuefen_text;
    @BoundView(R.id.grade) private TextView grade;
    @BoundView(R.id.next_degree) private TextView next_degree;

    //学位介绍
    private PostDegreeIndex postDegreeIndex = new PostDegreeIndex(new AsyCallBack<PostDegreeIndex.PostDegreeIndexInfo>() {

        @Override
        public void onSuccess(String toast, int type, Object o, PostDegreeIndex.PostDegreeIndexInfo postDegreeIndexInfo) throws Exception {
            next_degree.setText("距离"+postDegreeIndexInfo.next+":还需"+postDegreeIndexInfo.need+"学分");
            xuefen_text.setText(postDegreeIndexInfo.degree);
            grade.setText(postDegreeIndexInfo.xuewei);
            float total = Float.parseFloat(postDegreeIndexInfo.degree)/(Float.parseFloat(postDegreeIndexInfo.degree)+Float.parseFloat(postDegreeIndexInfo.need));

            if(postDegreeIndexInfo.degree.equals("0")){
                deep_pink.setVisibility(View.INVISIBLE);
                deep_pink.getLayoutParams().width = (int) (qian_pink.getLayoutParams().width *0);
            }else {
                deep_pink.setVisibility(View.VISIBLE);
                deep_pink.getLayoutParams().width = (int) (qian_pink.getLayoutParams().width *total);
            }

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);//定义一个LayoutParams

            layoutParams.setMargins(((int) ((qian_pink.getLayoutParams().width *total)+25)),((int) (qian_pink.getLayoutParams().width *0.66)),0,0);//4个参数按顺序分别是左上右下

            jindu_text.setLayoutParams(layoutParams); //mView是控件
            jindu_text.setText(postDegreeIndexInfo.degree);
            begin.setText(postDegreeIndexInfo.start);
            end.setText(postDegreeIndexInfo.end);
            if(total >=0 && total< 0.25){
                one.setImageResource(R.mipmap.introduce_yuan);
                two.setImageResource(R.mipmap.introduce_quan);
                three.setImageResource(R.mipmap.introduce_quan);
                four.setImageResource(R.mipmap.introduce_quan);
                five.setImageResource(R.mipmap.introduce_quan);
            }else if(total >=0.25 && total< 0.5){
                one.setImageResource(R.mipmap.introduce_yuan);
                two.setImageResource(R.mipmap.introduce_yuan);
                three.setImageResource(R.mipmap.introduce_quan);
                four.setImageResource(R.mipmap.introduce_quan);
                five.setImageResource(R.mipmap.introduce_quan);
            }else if(total >=0.5 && total< 0.75){
                one.setImageResource(R.mipmap.introduce_yuan);
                two.setImageResource(R.mipmap.introduce_yuan);
                three.setImageResource(R.mipmap.introduce_yuan);
                four.setImageResource(R.mipmap.introduce_quan);
                five.setImageResource(R.mipmap.introduce_quan);
            }else if(total >=0.75 && total< 1.00){
                one.setImageResource(R.mipmap.introduce_yuan);
                two.setImageResource(R.mipmap.introduce_yuan);
                three.setImageResource(R.mipmap.introduce_yuan);
                four.setImageResource(R.mipmap.introduce_yuan);
                five.setImageResource(R.mipmap.introduce_quan);
            }else if(total == 1.00){
                one.setImageResource(R.mipmap.introduce_yuan);
                two.setImageResource(R.mipmap.introduce_yuan);
                three.setImageResource(R.mipmap.introduce_yuan);
                four.setImageResource(R.mipmap.introduce_yuan);
                five.setImageResource(R.mipmap.introduce_yuan);
            }


            if(type == 0){
                list.clear();
            }


            list.addAll(postDegreeIndexInfo.list);
            degreeAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_degree_introduce);

        setBackTrue();
        initView();
    }

    private void initView(){
        setTitleName("学位介绍");

        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;         // 屏幕宽度（像素）

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        degreeAdapter = new DegreeAdapter(this, list);
        recyclerView.setAdapter(degreeAdapter);

        postDegreeIndex.user_id = BaseApplication.BasePreferences.readUID();
        postDegreeIndex.execute();
//        Log.e("=====jin",width + "--width");

    }
}
